/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.action;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;

import java.util.Iterator;
import java.util.List;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class DeleteEmptyChangelistAction extends PerforceAction {
  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void run(IAction action) {
    IChangelist[] changelists = getSelectedChangelists();
    Policy.assertTrue(changelists.length == 1);

    IChangelist changelist = changelists[0];
    if (confirmOperation(changelist)) {
      IPerforceServer server = changelist.getServer();
      try {
        server.deleteChangelist(changelist);
      } catch (PerforceException e) {
        handle(e);
      } finally {
        IPerforceServer[] servers = {server};
        PerforceCorePlugin.getPlugin().broadcastChangelistStateChanges(servers);
      }
    }
  }

  // ---------------------------------------------------------------------------
  // Protected Methods

  @Override
  protected boolean isEnabled() {
    IChangelist[] changelists = getSelectedChangelists();
    if (changelists.length != 1) {
      return false;
    }

    IChangelist changelist = changelists[0];
    if (changelist.getId() == IChangelist.DEFAULT) {
      return false;
    }

    IP4File[] files = changelist.getFiles();
    if (files.length > 0) {
      return false;
    }

    return true;
  }

  //----------------------------------------------------------------------------
  // - Private Methods

  /**
   * Called to get the user's confirmation for the operation.
   * 
   * @param changelist the changelist to delete.
   * @return true if the user wants to continue, false otherwise.
   */
  private boolean confirmOperation(IChangelist changelist) {
    String title = Policy.bind("DeleteEmptyChangelistAction.confirmationTitle"); //$NON-NLS-1$
    String message = Policy.bind("DeleteEmptyChangelistAction.confirmationMessage", //$NON-NLS-1$
        changelist.getId());

    return MessageDialog.openConfirm(getShell(), title, message);
  }

  /**
   * Called to retrieve an array of selected changelists.
   * 
   * @return array of selected changelists.
   */
  private IChangelist[] getSelectedChangelists() {
    List<IChangelist> resources = null;
    if (!getSelection().isEmpty()) {
      resources = Lists.newArrayList();
      for (Iterator<?> elements = getSelection().iterator(); elements.hasNext();) {
        Object next = elements.next();
        if (next instanceof IChangelist) {
          resources.add((IChangelist) next);
          continue;
        } else if (next instanceof IAdaptable) {
          Object adapter = ((IAdaptable) next).getAdapter(IChangelist.class);
          if (adapter instanceof IChangelist) {
            resources.add((IChangelist) adapter);
            continue;
          }
        }
      }
    }

    if (resources != null && !resources.isEmpty()) {
      return resources.toArray(new IChangelist[resources.size()]);
    }

    return new IChangelist[0];
  }
}
